package com.spirit.mypopup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import java.util.Random;

public class Main5Activity extends AppCompatActivity {

    private NotificationManager notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        createNotificationChannel("subscribe", "1", NotificationManager.IMPORTANCE_HIGH);

    }

    public void show(View view) {

        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                sendSubscribeMsg();
            }
        }, 2000);
    }


    private void sen() {
        NotificationCompat.Builder notifyBuilder =
                new NotificationCompat.Builder(this).setContentTitle("title")
                        .setContentText("内通")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        // 点击消失
                        .setAutoCancel(true)
                        // 设置该通知优先级
                        .setPriority(Notification.PRIORITY_MAX)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))

                        // 通知首次出现在通知栏，带上升动画效果的
                        .setWhen(System.currentTimeMillis())
                        // 通知产生的时间，会在通知信息里显示
                        // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：
                        .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND);
        /*PendingIntent resultPendingIntent =
                PendingIntent.getActivity( mContext, 0, mResultIntent, PendingIntent.FLAG_UPDATE_CURRENT );
        notifyBuilder.setContentIntent( resultPendingIntent );*/

        notificationManager.notify(new Random().nextInt(), notifyBuilder.build());
    }


    private void www() {


    }


    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setShowBadge(true);
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        // 设置通知出现时的震动（如果 android 设备支持的话）
        channel.enableVibration(true);
        channel.setBypassDnd(true);    //设置绕过免打扰模式
        channel.canBypassDnd();      //检测是否绕过免打扰模式
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        AudioAttributes build = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT).build();

        channel.setSound(resourceIdToUri(this, R.raw.fen_pei), build);

        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

        notificationManager.createNotificationChannel(channel);
    }

    public void sendSubscribeMsg() {

        Notification notification = new NotificationCompat.Builder(this, "subscribe")
                .setContentTitle("收到一条聊天消息")
                .setContentText("今天中午吃什么？")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.test)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.test))
                .setAutoCancel(true)
                .setNumber(2)
                .setOngoing(true)
                .build();

        notificationManager.notify(new Random().nextInt(), notification);
    }


    private final String ANDROID_RESOURCE = "android.resource://";

    private final String FOREWARD_SLASH = "/";

    private Uri resourceIdToUri(Context context, int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);

    }

}
